import {message} from 'antd';
import { query, fetchItem,sendItem,express, queryMenus, addItem, changeItem,cancelItem, removeItem } from '../services/shop_order';

export default {
  namespace: 'shop_order',

  state: {
      data: {list: [], pagination: {size: 10, current: 1, total: 0}},
      categories: [],
      options: [],
      item: {user: {}, address: {}, goodsArray: []},
      loading: false,
      maskLoading: false,
      shipModal:false,
      shipLoading:false,
      expressData:{}
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: Array.isArray(response.data) ? response : [],
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
      *queryMenus({_}, { call, put }) {
          const response = yield call(queryMenus, {pid: 0});
          const data = response.data;
         const options = data.map((item) => {
             const children = item.child.map((sub) => {
                  sub.value = sub.id;
                  sub.label = sub.name;
                  return sub;
              });
              item.value = item.id;
              item.label = item.name;
              item.children = children;
              return item;
          });

          yield put({
              type: 'saveCategory',
              payload: Array.isArray(response.data) ? response : [],
          });
          yield put({
              type: 'saveOptions',
              payload: Array.isArray(options) ? options : [],
          });
      },
      *fetchService({payload, callback}, {call, put}) {
          yield put({
              type: 'changeMaskLoading',
              payload: true,
          });
          const response = yield call(fetchItem, payload.id);
          if (response.status) {
              const data = response.data;
              yield put({
                  type: 'saveServiceData',
                  payload: data,
              });
              if (callback) {
                  callback(response.data);
              }
          }
          yield put({
              type: 'changeMaskLoading',
              payload: false,
          });
      },
      *fetchExpress({payload, callback}, {call, put}) {
        yield put({
            type: 'changeMaskLoading',
            payload: true,
        });
        const response = yield call(express, payload);
        if (response.status) {
            const data = response.data;
            yield put({
                type: 'saveExpressData',
                payload: data,
            });
            if (callback) {
                callback(data);
            }
        }
        yield put({
            type: 'changeMaskLoading',
            payload: false,
        });
    },
      *edit({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(changeItem, payload);
          if (response.status) {
              message.success('编辑成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *add({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(addItem, payload);
          if (response.status) {
              message.success('添加成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },

      *send({payload,callback},{call,put}) {
        yield put({
            type: 'changeShipLoading',
            payload: true,
        });
        const response = yield call(sendItem, payload);
        if (response.status) {
            message.success('发货成功');
            if (callback) {
                callback();
            }
        } else {
            message.error(response.message);
        }
        yield put({
            type: 'changeShipLoading',
            payload: false,
        });
      },

      *cancel({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(cancelItem, payload);
          if (response.status) {
              message.success('取消成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *remove({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(removeItem, payload);
          if (response.status) {
              message.success('删除成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
  },

  reducers: {
      save(state, action) {
      return {
        ...state,
        data: {list: action.payload.data, pagination: {pageSize: action.payload.page.size, total: action.payload.page.total, current: action.payload.page.current}},
      };
    },
      saveCategory(state, action) {
          return {
              ...state,
              categories: action.payload.data,
          };
      },
      saveOptions(state, action) {
          return {
              ...state,
              options: action.payload,
          };
      },
      saveServiceData(state, action) {
          return {
              ...state,
              item: action.payload,
          };
      },
      saveExpressData(state, action) {
        return {
            ...state,
            expressData: action.payload,
        };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
      changeMaskLoading(state, action) {
          return {
              ...state,
              maskLoading: action.payload,
          };
      },
      changeShipModal(state, action) {
        return {
            ...state,
            shipModal: action.payload,
        };
    },
    changeShipLoading(state, action) {
        return {
            ...state,
            shipLoading: action.payload,
        };
    },
},
};
